package com.khy.hyxx;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.provider.MediaStore.Images.Media;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class LookImageActivity extends Activity{  
    /**列表视图*/  
    private ListView listView;  
    /**图片对象*/  
    private ImageView image;  
    /**图片名字*/  
    private String name;  
    /**图片名字集合*/  
    private ArrayList<String> list = new ArrayList();  
    /**图片对象集合*/  
    private ArrayList<HashMap<String,Object>> picList = new ArrayList();  
      
    /**  
     * 初始化  
     */  
    public void initData(){  
        //查询SD卡所有图片，得到游标  
        Cursor cursor = getContentResolver().query(Media.EXTERNAL_CONTENT_URI, null, null, null, null);  
        while(cursor.moveToNext()){  
            //得到图片的名字  
            String name = cursor.getString(cursor.getColumnIndex(Media.DISPLAY_NAME));  
            //得到图片的描述信息  
            String info = cursor.getString(cursor.getColumnIndex(Media.DESCRIPTION));  
            //图片数据  
            byte[] data = cursor.getBlob(cursor.getColumnIndex(Media.DATA));  
            HashMap map = new HashMap();  
            map.put("name", name==null ? "":name);  
            map.put("info", info==null ? "":info);  
            map.put("data", data);  
            picList.add(map);  
        }  
    cursor.close();  
    }  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);       
        setContentView(R.layout.look_iamge_layout);  
        //调用初始化方法  
        initData();  
        listView = (ListView) findViewById(R.id.listView1);  
        //创建适配器  
        BaseAdapter adapter = new BaseAdapter() {  
              
            @Override  
            public View getView(int position, View convertView, ViewGroup parent) {  
                  
                if(convertView==null){  
                    convertView = getLayoutInflater().inflate(R.layout.look_image_listview_content, null);  
                }  
                image = (ImageView) convertView.findViewById(R.id.picture_list_content_iv_pic);  
                TextView nameText = (TextView) convertView.findViewById(R.id.picture_list_content_tv_name);  
                TextView infoText = (TextView) convertView.findViewById(R.id.picture_list_content_tv_info);  
                  
                name = picList.get(position).get("name").toString();  
                String info = picList.get(position).get("info").toString();  
                byte[] bs = (byte[]) picList.get(position).get("data");  
                //设置图片信息  
                nameText.setText(name);  
                infoText.setText(info);  
                //设置图片  
                Bitmap bitmap = BitmapFactory.decodeFile(new String(bs,0,bs.length-1));  
                image.setImageBitmap(bitmap);  
                //设置标识符  
                image.setTag(position);  
                list.add(name);  
                  
                //为图片添加监听  
                image.setOnClickListener(new OnClickListener() {  
                      
                    @Override  
                    public void onClick(View v) {  
                          
                        Intent intent = new Intent(LookImageActivity.this,LargeImageActivity.class);  
                        int id =Integer.parseInt(v.getTag().toString());  
                        intent.putExtra("id", id);  
                        intent.putExtra("list", list);  
                        startActivity(intent);  
                    }  
                });  
                return convertView;  
            }  
              
            @Override  
            public long getItemId(int position) {  
                return position;  
            }  
              
            @Override  
            public Object getItem(int position) {  
                return picList.get(position);  
            }  
              
            @Override  
            public int getCount() {  
                return picList.size();  
            }

        };  
          
        listView.setAdapter(adapter);  
    }  
}